<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="@/assets/images/logo.png" alt="avatar" />
      </div>
      <el-form
        class="login_form"
        :model="loginForm"
        :rules="loginRules"
        ref="loginFormRef"
        label-width="0"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            :show-password="true"
            placeholder="请输入密码"
          />
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {
  reqLogin
} from '../../api'

export default {
  name: 'login',
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        // 验证用户名、密码是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 5,
            max: 10,
            message: '长度在 5 到 10 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 10,
            message: '长度在 6 到 10 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 点击按钮，重置登录表单
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 点击按钮，登录
    login() {
      this.$refs.loginFormRef.validate(async valid => {
        // 验证成功，发送ajax请求
        if (!valid) return
        const { username, password } = this.loginForm
        const { data, meta } = await reqLogin({ username, password })
        // 登录失败
        if (meta.status !== 200) return this.$message.error(meta.msg)
        // 登录成功
        // 1.将登录成功后的token，保存到客户端的 sessionstorage中
        //  1.1项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //  1.2 token 只应在当前网站打开期间生效，所以将 token保存在sessionstorage中
        // 2.通过编程式导航跳转到后台主页，路由地址是/home
        this.$message.success(meta.msg)
        window.sessionStorage.setItem('token', data.token)
        window.localStorage.setItem('uid', data.id)
        this.$store.dispatch('recordUser', { data, meta })
        this.$router.replace('/home')
      })
    }
  }
}
</script>

<style lang="less" scoped>
  .login_container {
    background-color: #395a7ce8;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: relative;
  }

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    padding: 10px;
    border-radius: 50%;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10;
    box-shadow: 0 0 10px #ddd;
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #e7f5e0;
    }
  }

  .login_form {
    width: 100%;
    padding: 0 30px;
    box-sizing: border-box;
    position: absolute;
    bottom: 0;
  }

  .btns {
    display: flex;
    justify-content: center;
  }
</style>
